package com.pan.service.aspect;


import com.pan.dao.goods.GoodsSelectDao;
import com.pan.dao.goodsImg.GoodsImgsSelectDao;
import com.pan.entity.Goods;
import com.pan.entity.GoodsImgs;
import com.pan.entity.Result;
import com.pan.excep.enume.ErrorCode;
import com.pan.excep.error.ServiceException;
import com.pan.tools.RedisTools;
import com.pan.vo.GoodsImgsInsertVo;
import io.swagger.models.auth.In;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
public class GoodsImgAspect {

    @Resource
    private RedisTools redisTools;

    @Resource
    private GoodsImgsSelectDao goodsImgsSelectDao;

    @Resource
    private GoodsSelectDao goodsSelectDao;


    @Pointcut("execution(public * com.pan.service.impl.GoodsImgServiceImpl.updateSort(..))")
    public void updateSortCut(){}

    @Pointcut("execution(public * com.pan.service.impl.GoodsImgServiceImpl.delete(..))")
    public void deleteCut(){}

    @Pointcut("execution(public * com.pan.service.impl.GoodsImgServiceImpl.insert(..))")
    public void insertCut(){}

    public Integer indexOf(String[] names,String name){
        for (int i=0;i<names.length;i++){
            if (names[i].equals(name)){
                return i;
            }
        }
        return null;
    }


    @Order(2)
    @Before("updateSortCut()")
    public void checkUpdateNull(JoinPoint joinPoint){
        String[] names = ((MethodSignature)joinPoint.getSignature()).getParameterNames();
        Integer index = indexOf(names,"id");
        if (index == null){
            throw new ServiceException(ErrorCode.ASPECT_ERROR);
        }
        Object[] args = joinPoint.getArgs();
        Long id = (Long) args[index];
        GoodsImgs goodsImg = goodsImgsSelectDao.selectById(id);
        if (goodsImg==null){
            throw new ServiceException(ErrorCode.NOT_FOUND);
        }
    }

    @Before("insertCut()")
    @Order(2)
    public void checkInsertNull(JoinPoint joinPoint){
        String[] names = ((MethodSignature)joinPoint.getSignature()).getParameterNames();
        Integer index = indexOf(names,"goodsImgsInsertVo");
        if (index == null){
            throw new ServiceException(ErrorCode.ASPECT_ERROR);
        }
        Object[] args = joinPoint.getArgs();
        GoodsImgsInsertVo goodsImgsInsertVo = (GoodsImgsInsertVo) args[index];
        Long goodsId = goodsImgsInsertVo.getGoodsId();
        Goods goods = goodsSelectDao.selectById(goodsId);
        if (goods== null){
            throw new ServiceException(ErrorCode.NOT_FOUND);
        }
    }


    @Before("updateSortCut()||deleteCut()||insertCut()")
    @Order(1)
    public void checkLogin(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String[] names = methodSignature.getParameterNames();
        Integer index = indexOf(names,"adminTicket");
        if (index == null){
            throw new ServiceException(ErrorCode.ASPECT_ERROR);
        }
        String adminTicket = (String) args[index];
        Result<Boolean> result = redisTools.exists("admin:"+adminTicket);
        if (result==null||!result.getData()){
            throw  new ServiceException(ErrorCode.NOT_LOGIN);
        }

    }

}
